/************************************************************************/
/* SHARED FUNCTIONS														*/
/************************************************************************/

/* Get a player by nick */
player.GetByNick = function( nick, ... )

	/* Should return all matching players ? */
	local allMatches = select( 1, ... )

	/* Get all players */
	local players = player.GetAll()
	
	/* Loop players */
	local matches = {}
	for i,ply in ipairs(players) do
	
		/* Check if the player's nick contains the given string */
		if string.match( ply:Nick(), nick ) then
		
			/* If we want all matches, add it to the list */
			if allMatches then
				table.insert( matches, ply )
				
				/* Debug output */
				DEBUG:Msg( "Player found: '" .. ply:Nick() .. "' matching '" .. nick .. "'", "player.GetByNick" )
			
			/* Otherwise, immediately return the found player */
			else
				
				/* Debug output */
				DEBUG:Msg( "Returning first player found: '" .. ply:Nick() .. "' matching '" .. nick .. "'", "player.GetByNick" )
				
				return ply
			end
		end
	end
	
	/* Return all matches found */
	return matches
end

/* Get by steam ID */
player.GetBySteamID = function( steamID )

	/* Get all players */
	local players = player.GetAll()
	
	/* Loop players */
	for i,ply in ipairs(players) do
	
		/* Check if the player's steam IDis the same as the given one */
		if ply:SteamID() == steamID then
		
			/* Debug output */
			DEBUG:Msg( "Returning player found: '" .. ply:Nick() .. "' with steam ID '" .. steamID .. "'", "player.GetBySteamID" )
			
			return ply
		end
	end
	
	/* If not found, return false */
	return false
end

/* Checks whether a player is valid or not */
player.IsValid = function( ply )	
	return ply and type( ply ) == "Player" and IsValid( ply ) and ply:IsPlayer()
end

/* Checks whether a player is valid or not */
player.IsMemberOf = function( ply, teamObj )
	
	/* Debug output */
	DEBUG:Msg( "Checking player's team against " .. tostring( teamObj ), "player.IsMemberOf" )
	
	/* Not a valid player */
	if not player.IsValid( ply ) then
		DEBUG:Msg( "Invalid player: " .. tostring( ply ), "player.IsMemberOf" )
		return false
	end
	
	/* Test */
	DEBUG:Msg( "Team ID type: " .. tostring( type( team.GetInfo( teamObj ).ID ) ), "player.IsMemberOf" )
	
	/* Checks player's team */
	return (ply:Team() == team.GetInfo( teamObj ).ID)
end

/* Checks whether a player is an attacker */
player.IsAttacker = function( ply )
	return player.IsMemberOf( ply, "Attackers" )
end

/* Checks whether a player is an attacker */
player.IsDefender = function( ply )
	return player.IsMemberOf( ply, "Defenders" )
end

/* Checks whether a player is an attacker */
player.IsPrisoner = function( ply )
	return player.IsMemberOf( ply, "Prisoners" )
end

/* Checks whether a player is a spectator */
player.IsSpectator = function( ply )
	return player.IsMemberOf( ply, "Spectators" )
end

/************************************************************************/
/* SERVER ONLY															*/
/************************************************************************/

if SERVER then

/* Query player info */
player.QueryPlayerInfo = function( ply )

	/* Associate function to player if needed */
	if not ply.QueryPlayerInfo then
		ply.QueryPlayerInfo = function() player.QueryPlayerInfo( self ) end
	end

	/* Try to load player info */
	return LoadPlayerInfo( ply )
end

end

/************************************************************************/
/* CLIENT ONLY															*/
/************************************************************************/

if CLIENT then

end